<?php
/*
 * Created on Sep 13, 2010
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */
 
include_once("./configure/configure.php");		//--> global var
include_once("customerSession.inc.php");
include_once("Smarty.class.php");				//--> out template
include_once("Common.php");
include_once("UploadFiles.php");				//--> UploadFiles
include_once("Customer.php");					//--> Customer
include_once("Country.php");					//--> Country
include_once("AddressBook.php");				//--> AddressBook
include_once("Validation.php");					//--> Validation
include_once ("Session.php");					//-- Session
include_once ("Password.php");					//-- Password

include_once("Strings.php");					//--> String utils

include_once("Zone.php");						//--> Zone


include_once("orders/Cart.php");					//--> Cart
include_once("orders/CartProduct.php");				//--> Cart
include_once("orders/CartProductAttribute.php");	//--> Cart

$common 		= new Common();
$objPassword 	= new Password();
$objCountry		= new Country(0);						//--> Country
$objCustomer	= new Customer(DB_TAG_SYSTEM, $uid);	//--> Customer
$objAddressBook	= new AddressBook($uid);				//--> AddressBook
$objValidation	= new Validation($uid);					//--> Validation
$ins_session	= new Session(DB_TAG_SYSTEM, SESSION_TABLE_NAME);	//-- session

$objCart 		= new Cart();		//--> Cart
$objStrings 	= new Strings();
$objZone		= new Zone(0);		//--> Zone


session_start();

if($_SESSION['cart'])
	$objCart = unserialize($_SESSION['cart']);


$customerid 	= 0;
$zoneid			= 0;

if(isset($_POST['country']))
	$country	= $_POST['country'];
else
	$country	= 223;

if(isset($_POST['email_address'])){
	$firstname 		= $_POST['firstname'];
	$lastname 		= $_POST['lastname'];
	$email 			= $_POST['email_address'];
	
	$telephone		= $_POST['telephone'];
	$fax			= $_POST['fax'];
		
	$country		= $_POST['country'];
	$company		= $_POST['company'];
	$postcode		= $_POST['postcode'];
	
	$state			= $_POST['state'];
	$zoneid			= $_POST['zoneid'];
	
	$city			= $_POST['city'];
	$street			= $_POST['street_address'];
			
	$newsletter		= $_POST['newsletter'];
	
	$password		= $_POST['password'];
	$confirmation	= $_POST['confirmation'];
	
	$address_id		= 0;
	
	if(empty($newsletter))
		$newsletter = 'unsubscribed';
		
	if(empty($firstname))
		$error_message = 'The first name should\'t be empty.';
	else if(empty($lastname)){
		$error_message = 'The first name should\'t be empty.';
	}else if(empty($email)){
		$error_message = 'The email should\'t be empty.';
	}else if(!$objValidation->isEmail($email)){
		$error_message = 'The email format invalid.';
	}else if($objCustomer->IsExistEmail($email)){
		$error_message = 'Your E-Mail Address already exists in our records - please log in with the e-mail address or create an account with a different address.';
	}
	
	if(empty($state) && !empty($zoneid)){
		$sql = "SELECT name FROM zones WHERE id = $zoneid";
		$state = $common->getColumn(DB_TAG_PUBLIC, $sql);
	}else
		$zoneid = 0;
	
	if(empty($error_message)){
		$passwordmd5= $objPassword->encrypt($password);
		
		$id = $objCustomer->add($gender, $firstname, $lastname, $email, $address_id, $telephone, $fax, $newsletter, $passwordmd5, $country);
		if($id){
			$address_id = $objAddressBook->add($id, $gender, $company, $firstname, $lastname, $street, $suburb, $postcode, $city, $state, $country, $zoneid);
			
			$sql = "UPDATE customers SET modifiedTime = UTC_TIMESTAMP(), default_address_id = '$address_id' WHERE id = $id";
			$isSuccess = $common->update(DB_TAG_SYSTEM, $sql);
		
			
			$error_message = 'register successfully.';
			
			$emailContent = "<div><br>
								Dear $lastname<br>
								<br>
								We welcome you to <a href=\"http://123gohelmets.com\" target=\"_blank\">123gohelmets.com</a>.<br>
								<br>
								You can now take part in the various services we have to offer you. Some of these services include:<br>
								<br>
								Permanent Cart - Any products added to your online cart remain there until you remove them, or check them out.<br>
								Address Book - We can now deliver your products to another address other than yours! This is perfect to send birthday gifts direct to the birthday-person themselves.<br>
								Order History - View your history of purchases that you have made with us.<br>
								Products Reviews - Share your opinions on products with our other customers.<br>
								<br>
								For help with any of our online services, please email the store-owner: <a href=\"mailto:atvglove@gmail.com\">atvglove@gmail.com</a>.<br>
								<br>
								Note: This email address was given to us by one of our customers. If you did not signup to be a member, please send an email to <a href=\"mailto:atvglove@gmail.com\">atvglove@gmail.com</a>.<br>
								<br>
								<br>
								</div>";
			
			$subject	= 'Welcome to 123gohelmets.com';
			
			/* To send HTML mail, you can set the Content-type header. */
			$headers  = "MIME-Version: 1.0\r\n";
			$headers .= "Content-type: text/html; charset=utf8\r\n";
			
			/* additional headers */
			$headers .= "From: Sam <atvglove@gmail.com>\r\n";
			
			/* and now mail it */
			@mail($email, $subject, $emailContent, $headers);
			
			$arrdata = array();
			$arrdata['uname']	= $lastname;		//--> add login name to session
		
			if(is_array($ins_session->start($id, $arrdata))){
			}
			
			$location = HOME_URL . '/myaccount.php';
	
			header("Location: $location");
			exit;
		}else{
			$error_message = 'register failure.';
		}
	}
}


include_once("includeCategory.php");	//--> include category
include_once("includeSpec.php");		//--> include spec

$arrCountries = $objCountry->getGroupList();

$arrZones = $objZone->getGroupList($country);
if(count($arrZones) > 0)
	$hasZone = 'yes';

/*----- out html -----*/
$smarty = new Smarty();	//-- out template
$smarty->template_dir 	= TEMPLATE_DIR;
$smarty->compile_dir  	= COMPILE_DIR;
$smarty->cache_dir  	= CACHE_DIR;

$smarty->caching 		= CACHING;
$smarty->cache_lifetime = CACHE_LIFETIME;

$smarty->assign('HOME_URL', HOME_URL);
$smarty->assign('HOME_URL_HTTP', HOME_URL);

$smarty->assign('error_message', $error_message);

$smarty->assign('categorys', $categorys);
$smarty->assign('topCategory', $topCategory);
	
$smarty->assign('specProducts', $specProducts);

$smarty->assign('country', $country);
$smarty->assign('arrCountries', $arrCountries);

$smarty->assign('hasZone', $hasZone);
$smarty->assign('arrZones', $arrZones);
$smarty->assign('zoneid', $zoneid);

$smarty->assign('firstname', $firstname);
$smarty->assign('lastname', $lastname);
$smarty->assign('email_address', $email);

$smarty->assign('telephone', $telephone);
$smarty->assign('fax', $fax);
$smarty->assign('company', $company);
$smarty->assign('postcode', $postcode);
$smarty->assign('state', $state);
$smarty->assign('city', $city);
$smarty->assign('street_address', $street);

$smarty->assign('objCart', $objCart);
$smarty->assign('objStrings', $objStrings);

$smarty->display('register.html');
?>
